package com.zpark.rabbitmq.subscription;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.zpark.rabbitmq.config.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @PackageName: com.zpark.rabbitmq.subscription
 * @ClassName: Producer
 * @Description:
 * @author: RZS
 * @date: 2022/12/13  17:01
 */
public class Producer {

    // 定义交换机名字
    private static final String EXCHANGE_NAME = "test_exchange_fanout";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 获得连接
        Connection conn = ConnectionUtil.getConnection();
        // 建立通道
        Channel channel = conn.createChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        for (int i=0; i<10; i++) {
            // 创建要发送的消息
            String msg = "Hello, World! " + i;
            // 发送消息
            channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
            // 方便测试，打印输出
            System.out.println("生产者：发送消息 msg -> " + msg);
        }
        // 关闭通道
        channel.close();
        // 关闭连接
        conn.close();
    }
}
